package com.sita.tboard.hitchhike.helper;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.sita.bike.persistence.CcDriverEntity;
import com.sita.bike.persistence.CcDriverEntityDao;
import com.sita.bike.persistence.CcDriverTicketEntity;
import com.sita.bike.persistence.CcDriverTicketEntityDao;
import com.sita.bike.persistence.CcPassengerTicketEntity;
import com.sita.bike.persistence.CcPassengerTicketEntityDao;
import com.sita.bike.support.GlobalContext;
import com.sita.bike.utils.AccountUtils;
import com.sita.tboard.hitchhike.bean.Driver;
import com.sita.tboard.hitchhike.bean.TicketModel;
import com.sita.tboard.hitchhike.bean.TicketStatus;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xiaodong on 16/4/13.
 */
public enum HitchhikePersistHelper {
    INSTANCE;

    HitchhikePersistHelper() {
    }

    public Driver getDriver() {
        CcDriverEntityDao dao = GlobalContext.getDaoSession().getCcDriverEntityDao();
        CcDriverEntity entity = dao.load(Long.valueOf(AccountUtils.getAccountID()));
        Driver driver = new Driver();
        driver.id = entity.getId();
        driver.vehicleNumber = entity.getVehicleNumber();
        driver.vehicleBrand = entity.getVehicleBrand();
        return driver;
    }

    public void saveDriver(Driver driver) {
        CcDriverEntityDao dao = GlobalContext.getDaoSession().getCcDriverEntityDao();
        CcDriverEntity entity = new CcDriverEntity();
        entity.setId(driver.id);
        entity.setVehicleBrand(driver.vehicleBrand);
        entity.setVehicleNumber(driver.vehicleNumber);
        dao.insertOrReplace(entity);
    }

    public boolean isDriver() {
        CcDriverEntityDao dao = GlobalContext.getDaoSession().getCcDriverEntityDao();
        return dao.count() > 0;
    }

    public Cursor openDriverTicketUnfinishedCursor(){
        SQLiteDatabase db = GlobalContext.getDaoSession().getDatabase();
        String selection = CcDriverTicketEntityDao.Properties.DriverId.columnName + " = ? and "
                + CcDriverTicketEntityDao.Properties.Status.columnName + " in (1,3,4,5)";
        return db.query(CcDriverTicketEntityDao.TABLENAME, null, selection,
                new String[]{AccountUtils.getAccountID()},
                null, null, CcDriverTicketEntityDao.Properties.Id.columnName + " desc");
    }

    public Cursor openPassengerTicketUnfinishedCursor(){
        SQLiteDatabase db = GlobalContext.getDaoSession().getDatabase();
        String selection = CcPassengerTicketEntityDao.Properties.PassengerId.columnName + " = ? and "
                + CcPassengerTicketEntityDao.Properties.Status.columnName + " in (1,3,4,5)";
        return db.query(CcPassengerTicketEntityDao.TABLENAME, null, selection,
                new String[]{AccountUtils.getAccountID()},
                null, null, CcPassengerTicketEntityDao.Properties.Id.columnName + " desc");
    }

    public void updateDriverTicket(CcDriverTicketEntity entity) {
        CcDriverTicketEntityDao dao = GlobalContext.getDaoSession().getCcDriverTicketEntityDao();
        dao.update(entity);
    }

    public void saveDriverTickets(List<TicketModel> tickets) {
        int size = tickets.size();
        ArrayList<CcDriverTicketEntity> entities = new ArrayList<CcDriverTicketEntity>();
        for (int i = 0; i < size; i++) {
            TicketModel ticket = tickets.get(i);
            entities.add(ticketToDriverEntity(ticket));
        }
        CcDriverTicketEntityDao dao = GlobalContext.getDaoSession().getCcDriverTicketEntityDao();
        dao.insertOrReplaceInTx(entities);
    }

    public void savePassengerTickets(List<TicketModel> tickets) {
        int size = tickets.size();
        ArrayList<CcPassengerTicketEntity> entities = new ArrayList<CcPassengerTicketEntity>();
        for (int i = 0; i < size; i++) {
            TicketModel ticket = tickets.get(i);
            entities.add(ticketToPassengerEntity(ticket));
        }
        CcPassengerTicketEntityDao dao = GlobalContext.getDaoSession().getCcPassengerTicketEntityDao();
        dao.insertOrReplaceInTx(entities);
    }

    public void saveDriverTicket(TicketModel ticket) {
        CcDriverTicketEntityDao dao = GlobalContext.getDaoSession().getCcDriverTicketEntityDao();
        CcDriverTicketEntity entity = ticketToDriverEntity(ticket);
        dao.insertOrReplace(entity);
    }

    public void savePassengerTicket(TicketModel ticket) {
        CcPassengerTicketEntityDao dao = GlobalContext.getDaoSession().getCcPassengerTicketEntityDao();
        CcPassengerTicketEntity entity = ticketToPassengerEntity(ticket);
        dao.insertOrReplace(entity);
    }

    public CcDriverTicketEntity getDriverEntity(long id) {
        CcDriverTicketEntityDao dao = GlobalContext.getDaoSession().getCcDriverTicketEntityDao();
        return dao.load(id);
    }

    public TicketModel getDriverTicket(long id) {
        CcDriverTicketEntity entity = getDriverEntity(id);
        return driverEntityToModel(entity);
    }

    public CcPassengerTicketEntity getPassengerEntity(long id) {
        CcPassengerTicketEntityDao dao = GlobalContext.getDaoSession().getCcPassengerTicketEntityDao();
        return dao.load(id);
    }

    public TicketModel getPassengerTicket(long id) {
        CcPassengerTicketEntity entity = getPassengerEntity(id);
        return passengerEntityToModel(entity);
    }

    public void updatePassengerTicketStatus(long ticketId, int status) {
        updatePassengerTicketStatus(ticketId, status, null);
    }

    public void updatePassengerTicketStatus(long ticketId, int status, String driverId) {
        CcPassengerTicketEntityDao dao = GlobalContext.getDaoSession().getCcPassengerTicketEntityDao();
        CcPassengerTicketEntity entity = dao.load(ticketId);
        entity.setStatus(status);
        if (!TextUtils.isEmpty(driverId)) {
            entity.setDriverId(driverId);
        }
        dao.update(entity);
    }

    public void updateDriverTicketStatus(long ticketId, int status) {
        CcDriverTicketEntityDao dao = GlobalContext.getDaoSession().getCcDriverTicketEntityDao();
        CcDriverTicketEntity entity = dao.load(ticketId);
        entity.setStatus(status);
        dao.update(entity);
    }

    public List<CcDriverTicketEntity> getDriverTickets() {
        CcDriverTicketEntityDao dao = GlobalContext.getDaoSession().getCcDriverTicketEntityDao();
        return dao.queryBuilder().where(CcDriverTicketEntityDao.Properties.DriverId
                .eq(AccountUtils.getAccountID())).list();
    }

    public List<CcPassengerTicketEntity> getPassengerTickets() {
        CcPassengerTicketEntityDao dao = GlobalContext.getDaoSession().getCcPassengerTicketEntityDao();
        return dao.queryBuilder().where(CcPassengerTicketEntityDao.Properties.PassengerId
                .eq(AccountUtils.getAccountID())).list();
    }

    public void deleteDriverTicket(long ticketId) {
        CcDriverTicketEntityDao dao = GlobalContext.getDaoSession().getCcDriverTicketEntityDao();
        dao.deleteByKey(ticketId);
    }

    public void deleteDriverTickets() {
        CcDriverTicketEntityDao dao = GlobalContext.getDaoSession().getCcDriverTicketEntityDao();
        dao.queryBuilder().where(CcDriverTicketEntityDao.Properties.DriverId
                .eq(AccountUtils.getAccountID())).buildDelete().executeDeleteWithoutDetachingEntities();
    }

    public void deletePassengerTicket(long ticketId) {
        CcPassengerTicketEntityDao dao = GlobalContext.getDaoSession().getCcPassengerTicketEntityDao();
        dao.deleteByKey(ticketId);
    }

    public void deletePassengerTickets() {
        CcPassengerTicketEntityDao dao = GlobalContext.getDaoSession().getCcPassengerTicketEntityDao();
        dao.queryBuilder().where(CcPassengerTicketEntityDao.Properties.PassengerId
                .eq(AccountUtils.getAccountID())).buildDelete().executeDeleteWithoutDetachingEntities();
    }

    private CcPassengerTicketEntity ticketToPassengerEntity(TicketModel ticket) {
        CcPassengerTicketEntity entity = new CcPassengerTicketEntity();
        entity.setId(ticket.id);
        entity.setPassengerId(String.valueOf(ticket.customId));
        if (ticket.driverId != null) {
            entity.setDriverId(String.valueOf(ticket.driverId));
        }
        entity.setFromAddress(ticket.fromaddress);
        entity.setToAddress(ticket.toaddress);
        entity.setFromLocation(ticket.startLng + "," + ticket.startLat);
        entity.setToLocation(ticket.endLng + "," + ticket.endLat);
        entity.setDepartureTime(ticket.orderDepartureTime);
        if (ticket.money != null && !Double.isNaN(ticket.money)) {
            entity.setMoney(ticket.money.floatValue());
        }
        entity.setStatus(ticket.status);
        return entity;
    }

    private TicketModel passengerEntityToModel(CcPassengerTicketEntity entity) {
        TicketModel ticket = new TicketModel();
        ticket.id = entity.getId();
        ticket.customId = Long.valueOf(entity.getPassengerId());
        ticket.driverId = entity.getDriverId() != null ? Long.valueOf(entity.getDriverId()) : null;
        ticket.fromaddress = entity.getFromAddress();
        ticket.toaddress = entity.getToAddress();
        ticket.status = entity.getStatus();
        ticket.money = entity.getMoney() != null ? entity.getMoney().doubleValue() : null;
        ticket.orderDepartureTime = entity.getDepartureTime();
        String[] fLoc = TextUtils.split(entity.getFromAddress(), ",");
        String[] tLoc = TextUtils.split(entity.getToAddress(), ",");
        ticket.startLng = Double.valueOf(fLoc[0]);
        ticket.startLat = Double.valueOf(fLoc[1]);
        ticket.endLng = Double.valueOf(tLoc[0]);
        ticket.endLat = Double.valueOf(tLoc[1]);
        return ticket;
    }

    private CcDriverTicketEntity ticketToDriverEntity(TicketModel ticket) {
        CcDriverTicketEntity entity = new CcDriverTicketEntity();
        entity.setId(ticket.id);
        entity.setPassengerId(String.valueOf(ticket.customId));
        if (ticket.driverId != null) {
            entity.setDriverId(String.valueOf(ticket.driverId));
        }
        entity.setFromAddress(ticket.fromaddress);
        entity.setToAddress(ticket.toaddress);
        entity.setFromLocation(ticket.startLng + "," + ticket.startLat);
        entity.setToLocation(ticket.endLng + "," + ticket.endLat);
        entity.setDepartureTime(ticket.orderDepartureTime);
        if (ticket.money != null && !Double.isNaN(ticket.money)) {
            entity.setMoney(ticket.money.floatValue());
        }
        entity.setStatus(ticket.status);
        return entity;
    }

    private TicketModel driverEntityToModel(CcDriverTicketEntity entity) {
        TicketModel ticket = new TicketModel();
        ticket.id = entity.getId();
        ticket.customId = Long.valueOf(entity.getPassengerId());
        ticket.driverId = entity.getDriverId() != null ? Long.valueOf(entity.getDriverId()) : null;
        ticket.fromaddress = entity.getFromAddress();
        ticket.toaddress = entity.getToAddress();
        ticket.status = entity.getStatus();
        ticket.money = entity.getMoney() != null ? entity.getMoney().doubleValue() : null;
        ticket.orderDepartureTime = entity.getDepartureTime();
        String[] fLoc = TextUtils.split(entity.getFromAddress(), ",");
        String[] tLoc = TextUtils.split(entity.getToAddress(), ",");
        ticket.startLng = Double.valueOf(fLoc[0]);
        ticket.startLat = Double.valueOf(fLoc[1]);
        ticket.endLng = Double.valueOf(tLoc[0]);
        ticket.endLat = Double.valueOf(tLoc[1]);
        return ticket;
    }
}
